package com.db.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;

/**
 * JDBC连接工具类,连接信息写入配置文件
 *
 * @author ：Bruce
 * @date ：Created in 2019/12/10 22:21
 */
public class JdbcUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(JdbcUtils.class);

    private static Properties properties;

    private JdbcUtils() {
        throw new IllegalAccessError();
    }


    private static void initProperties() {
        properties = new Properties();
        String path = "D:\\code\\kingstar\\dfzq\\dfzq-dx\\kingstar-stream-dfzq\\dfzq-test\\src\\main\\resources\\db.properties";
//        String path = "db.properties";
        File file = new File(path);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            properties.load(fis);
            fis.close();
        } catch (Exception e) {
            LOGGER.error("init db properties error,can not read db.properties", e);
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 获取数据库连接<br>
     * 数据库连接属性存放于db.properties
     *
     * @return Connection
     */
    public static Connection getConnection() throws Exception {
        if (properties == null) {
            initProperties();
        }
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }


    /**
     * 获取配置文件
     */
    public static Properties getProperties() {
        if (properties == null) {
            initProperties();
        }
        return properties;
    }

    /**
     * 获取Oracle数据库连接
     *
     * @param url      url
     * @param username username
     * @param password password
     * @return Connection
     */
    public static Connection getOracleConnection(String url, String username, String password) throws Exception {
        Class.forName("oracle.jdbc.OracleDriver");
        return DriverManager.getConnection(url, username, password);
    }

    /**
     * 获取Mysql数据库连接
     *
     * @param url      url
     * @param username username
     * @param password password
     * @return Connection
     */
    public static Connection getMysqlConnection(String url, String username, String password) throws Exception {
        return DriverManager.getConnection(url, username, password);
    }

    /**
     * 执行DML操作
     *
     * @param sql   预编译sql语句
     * @param param 参数值
     */

    public static void execute(String sql, Object... param) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = getConnection();
            LOGGER.info("get conn success --> {}", conn);
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < param.length; i++) {
                ps.setObject(i + 1, param[i]);
            }
            ps.execute();
            LOGGER.info("sql execute success --> [{}]", sql);
        } catch (Exception e) {
            LOGGER.error("sql execute failed --> [{}]", sql, e);
        } finally {
            close(ps, conn);
        }
    }

    /**
     * 关闭所有
     *
     * @param autoCloseables 需关闭对象
     */
    public static void close(AutoCloseable... autoCloseables) {
        for (AutoCloseable autoCloseable : autoCloseables) {
            if (autoCloseable != null) {
                try {
                    autoCloseable.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
